package br.ufal.tci.nexos.arcolive.tests;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Vector;

import javax.media.CannotRealizeException;
import javax.media.NoDataSourceException;
import javax.media.NoPlayerException;
import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import br.ufal.tci.nexos.arcolive.beans.PlayerInternalFrame;
import br.ufal.tci.nexos.arcolive.beans.CannotJumpToPositionException;
import br.ufal.tci.nexos.arcolive.beans.CannotPlayStreamException;


public class LVSApplication extends JFrame {
    JDesktopPane desktop;
    int quantJanelas = 0;
    private Vector lvsInternalFrames;
    
    public LVSApplication() {
        super("LVSApplication");
        lvsInternalFrames = new Vector();
        
        JMenuBar menuBar = new JMenuBar();
        JMenu file = new JMenu("File");
        file.setMnemonic(KeyEvent.VK_F);
        JMenuItem open = new JMenuItem("Open...");
        open.setMnemonic(KeyEvent.VK_O);
        JMenuItem setPath = new JMenuItem("Set path...");
        setPath.setMnemonic(KeyEvent.VK_S);
        JMenuItem pause = new JMenuItem("Pause a video runing...");
        pause.setMnemonic(KeyEvent.VK_P);
        JMenuItem play = new JMenuItem("Play a video paused...");
        play.setMnemonic(KeyEvent.VK_L);
        JMenuItem repeat = new JMenuItem("Repeat a video...");
        repeat.setMnemonic(KeyEvent.VK_R);
        JMenuItem unrepeat = new JMenuItem("Unrepeat a video...");
        unrepeat.setMnemonic(KeyEvent.VK_U);
        JMenuItem jump = new JMenuItem("Jump to position...");
        jump.setMnemonic(KeyEvent.VK_J);
        JMenuItem foward = new JMenuItem("Foward/rewind a video...");
        foward.setMnemonic(KeyEvent.VK_F);
        file.add(open);
        file.add(repeat);
        file.add(unrepeat);
        file.add(play);
        file.add(setPath);
        file.add(pause);
        file.add(foward);
        file.add(jump);
        menuBar.add(file);
        setJMenuBar(menuBar);
        
        desktop = new JDesktopPane();
        setContentPane(desktop);
        desktop.setBackground(Color.BLACK);
        
        open.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                final JFileChooser openFile = new JFileChooser();
                openFile.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int result = openFile.showOpenDialog(desktop);
                
                if(result == JFileChooser.APPROVE_OPTION) {
                    try {
                        String path = openFile.getSelectedFile().toURL().toExternalForm();
                        play(path);
                    } catch (MalformedURLException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
        );
        
        repeat.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String path = JOptionPane.showInputDialog(null, "Path: ", "Path", JOptionPane.OK_CANCEL_OPTION);
                if (path != null) {
                    if (path.equals(""))
                        path = "vfw://0";
                    PlayerInternalFrame lvsInternalFrame = find(path);
                    if (lvsInternalFrame != null) {
                        lvsInternalFrame.getArcolivePlayer().setRepetitive(true);
                    }
                }
            }
            
        }
        
        );
        
        jump.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String path = JOptionPane.showInputDialog(null, "Path: ", "Path", JOptionPane.OK_CANCEL_OPTION);
                if (path != null) {
                    if (path.equals(""))
                        path = "vfw://0";
                    PlayerInternalFrame lvsInternalFrame = find(path);
                    if (lvsInternalFrame != null) {
                        String seconds = JOptionPane.showInputDialog(null, "Seconds: ", "Time", JOptionPane.OK_CANCEL_OPTION);
                        try {
							lvsInternalFrame.getArcolivePlayer().jumpToPosition(Integer.parseInt(seconds));
						} catch (NumberFormatException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (CannotJumpToPositionException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
                    }
                }
            }
            
        }
        
        );
        
        foward.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String path = JOptionPane.showInputDialog(null, "Path: ", "Path", JOptionPane.OK_CANCEL_OPTION);
                if (path != null) {
                    if (path.equals(""))
                        path = "vfw://0";
                    PlayerInternalFrame lvsInternalFrame = find(path);
                    if (lvsInternalFrame != null) {
                        String rate = JOptionPane.showInputDialog(null, "Rate: ", "Rate", JOptionPane.OK_CANCEL_OPTION);
                        lvsInternalFrame.getArcolivePlayer().setRate(Integer.parseInt(rate));
                    }
                }
            }
            
        }
        
        );
        
        unrepeat.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String path = JOptionPane.showInputDialog(null, "Path: ", "Path", JOptionPane.OK_CANCEL_OPTION);
                if (path != null) {
                    if (path.equals(""))
                        path = "vfw://0";
                    PlayerInternalFrame lvsInternalFrame = find(path);
                    if (lvsInternalFrame != null) {
                        lvsInternalFrame.getArcolivePlayer().setRepetitive(false);
                    }
                }
            }
            
        }
        
        );
        
        play.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String path = JOptionPane.showInputDialog(null, "Path: ", "Path", JOptionPane.OK_CANCEL_OPTION);
                if (path != null) {
                    if (path.equals(""))
                        path = "vfw://0";
                    PlayerInternalFrame lvsInternalFrame = find(path);
                    if (lvsInternalFrame != null) {
                        try {
							lvsInternalFrame.getArcolivePlayer().play();
						} catch (CannotPlayStreamException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
                    }
                }
            }
            
        }
        
        );
        
        setPath.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String path = JOptionPane.showInputDialog(null, "Path: ", "Path", JOptionPane.OK_CANCEL_OPTION);
                if (path != null) {
                    if (path.equals(""))
                        path = "vfw://0";
                    play(path);
                }
            }
        }
        );
        
        pause.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String path = JOptionPane.showInputDialog(null, "Path: ", "Path", JOptionPane.OK_CANCEL_OPTION);
                if (path != null) {
                    if (path.equals(""))
                        path = "vfw://0";
                    PlayerInternalFrame lvsInternalFrame = find(path);
                    if (lvsInternalFrame != null) {
                        lvsInternalFrame.getArcolivePlayer().pause();
                    }
                }
            }
            
        }
        );
        
        setSize(640, 480);
        setVisible(true);
    }
    
    public void play(String path) {
        PlayerInternalFrame janela = new PlayerInternalFrame();
        //janela.setPath(path);
        try {
            this.lvsInternalFrames.add(janela);
            janela.playLocalMedia();
            //Container telaInterna = janela.getContentPane();
            //telaInterna.setLayout(new BorderLayout());
            
            quantJanelas++;
            //janela.setTitle("Stream from: " + janela.getPath());
            
            janela.setSize(340, 295);
            janela.setLocation(20 * quantJanelas, 20 * quantJanelas);
            janela.setVisible(true);
            desktop.add(janela);
            
            //janela.setSelected(true);
        } catch (CannotPlayStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "Error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		}
    }
    
    public PlayerInternalFrame find(String path) {
        for(int i = 0; i < lvsInternalFrames.size(); i++) {
            PlayerInternalFrame lvsPlayerInternalFrame = (PlayerInternalFrame)lvsInternalFrames.elementAt(i);
            //if (lvsPlayerInternalFrame.getLvsPlayer().getPath().equals(path)) {
            //return lvsPlayerInternalFrame;
            //}
        }
        return null;
    }
    
    public static void main(String args[]){
        LVSApplication app = new LVSApplication();
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}